﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using STMS.BLL;
using STMS.Models.DModels;
using STMS.STMSApp.FModels;

namespace STMS.STMSApp.store
{
    public partial class StoreRegionInfoForm : Form
    {
        public StoreRegionInfoForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 刷新仓库列表数据事件
        /// </summary>
        public event Action ReLoadStoreList;
        /// <summary>
        /// 刷新仓库分区列表数据事件
        /// </summary>
        public event Action ReLoadStoreRegionList;

        private StoreRegionBLL srBLL = new StoreRegionBLL();
        private FInfoData fInfo = null;
        int actType = 1;//当前页面状态标识
        int regionId = 0;//当前修改的仓库编号
        string oldName = "";//修改前的分区名称
        string oldNo = "";//修改前的分区编码
        int oldStoreId = 0;//修改前的仓库编号

        private void StoreRegionInfoForm_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                fInfo = this.Tag as FInfoData;
                InitStoreRegionInfo();
            }
        }
        /// <summary>
        /// 初始化页面信息
        /// </summary>
        private void InitStoreRegionInfo()
        {
            if (fInfo != null)
            {
                actType = fInfo.ActType;
                FormUtility.LoadCboStores(cbo_store);
                string actName = "";
                switch (actType)
                {
                    case 1:
                        btn_clear.Enabled = true;
                        ClearInfo();
                        actName = "新增";
                        panel_tem.Visible = false;
                        break;
                    case 2:
                        regionId = fInfo.FId;
                        //加载仓库
                        btn_clear.Visible = false;
                        GetRegionInfo();
                        panel_tem.Visible = true;
                        actName = "修改";
                        break;
                    case 3:
                        btn_clear.Enabled = true;
                        ClearInfo();
                        cbo_store.SelectedValue = fInfo.FId;//StoreId
                        cbo_store.Enabled = false;
                        panel_tem.Visible = false;
                        actName = "新增";
                        break;
                }

                btn_submit.Text = actName;
                this.Text += "----" + actName;
            }
        }

        /// <summary>
        /// 加载指定的分区信息
        /// </summary>
        private void GetRegionInfo()
        {
            StoreRegionInfo srInfo = srBLL.GetStoreRegionInfo(regionId);
            if (srInfo != null)
            {
                txt_regionName.Text = srInfo.SRegionName;
                txt_regionNo.Text = srInfo.SRegionNo;
                cbo_store.SelectedValue = srInfo.StoreId;
                txt_nowTem.Text = srInfo.SRTemperature == null ? "" : srInfo.SRTemperature.Value.ToString();
                txt_lowTem.Text = srInfo.AllowLowTemperature == null ? "" : srInfo.AllowLowTemperature.Value.ToString();
                txt_highTem.Text = srInfo.AllowHighTemperature == null ? "" : srInfo.AllowHighTemperature.Value.ToString();
                txt_regionRemark.Text = srInfo.Remark;
                oldName = srInfo.SRegionName;
                oldNo = srInfo.SRegionNo;
                oldStoreId = srInfo.StoreId;
            }
        }

        /// <summary>
        /// 页面清空
        /// </summary>
        private void ClearInfo()
        {
            txt_regionName.Clear();
            txt_regionNo.Clear();
            cbo_store.SelectedIndex = 0;
            if (actType == 3)
                cbo_store.SelectedValue = fInfo.FId;
            txt_nowTem.Clear();
            txt_regionRemark.Clear();
            oldName = "";
            oldNo = "";
            oldStoreId = 0;
        }


    }
}
